<?php
namespace app\api\controller\v1;
use think\facade\{
    Config,Request,Cache
};
use app\common\facade\{
    User,Seller,Shop
};
use think\Db;
class Com extends Base
{
    /**
     * 获取手机验证码
     * @return [type] [token]
     */
    public function codePhone()
    {
        //验证输入
        $d = request()->post();
        $rule = [
            'phone'=>'require|mobile',
        ];
        $msg = [];
        $r = $this->validate($d, $rule,$msg);
        if($r!==true) return err($r);

        $model = $this->app->model('ComCodephone');
        $rs = $model->sendSms($d['phone']);
        if($rs)
            return suc(['phone_token'=>$rs[0], 'code'=>$rs[1]]);
        return err($model->getError());
    }
    /**
     * 获取图形验证码
     * @return [type] [token]
     */
    public function codeImg()
    {
        $token = com_token();
        $end_time = Request::time()+Config::get('type.expire_time');
        return suc(['img_token'=>$token,'url'=>url("/captcha/{$token}"),'end_time'=>$end_time]);
    }

    /**
     * 验证图片验证码是否正确
     * @return [type] [description]
     */
    public function checkCodeimg(){
        //验证输入
        $d = request()->post();
        $rule = [
            'img_token'=>'require',
            'code'=>'require',
        ];
        $msg = [];
        $r = $this->validate($d, $rule,$msg);
        if($r!==true) return err($r);

        $model = $this->app->model('ComCodeimg');
        $rs = $model->checkCode($d['img_token'],$d['code']);
        if($rs)
            return suc();
        return err($model->getError());
    }

    /**
     * 上传图片
     * @return [type] [description]
     */
    public function upimg(){
        if(is_post()){
            $file = request()->file('file');
            if($file->check(['size'=>20*1024*1024,'ext'=>'jpg,jpeg,png,gif'])){
                //验证宽高比
                $image  = \think\Image::open(request()->file('file'));
                $width  = $image->width();
                $height = $image->height();
                if($width/$height > 1920/400){
                    return err('宽高比不能大于: 1920/400');
                }
                $ip = $this->request->ip(1);
                $dir = './uploads/'.date('Y').'/'.date('m').'/'.date('d').'/';
                $filename = $dir.com_token($ip).".".$image->type();
                if (!is_dir(dirname($filename))) {
                    mkdir(dirname($filename), 0755, true);
                }
                $image->thumb(1920, 1920)->save($filename);
                $url = "/".$filename;
                $model = $this->app->model('ComAttachment');
                $model->url = $url;
                $model->save();
                return suc(['url'=>$url]);
            }
            return err($file->getError());
        }
    }

    /**
     * 获取登录用户信息
     * @return [type] [description]
     */
    public function getinfo(){
        $userinfo = $seller = $shop = [];
        $d = Request::post();
        if(isset($d['token'])){
            $userinfo = User::getInfoBytoken($d['token']);
        }
        if(isset($d['seller_token'])){
            $seller = Seller::getInfoBytoken($d['seller_token']);
        }
        if(isset($d['shop_token'])){
            $shop = Shop::getInfoBytoken($d['shop_token']);
        }

        if($userinfo){
            $info = $userinfo;
            //unset($info['password']);
        }elseif($shop){
            $info = $shop;
            $info['shop_category_id_info'] = Db::name('shop_category')->where('id',$shop['shop_category_id'])->value('title') ?:"";
            $info['shop_pic_0'] = Db::name('shop_pic')->where('shop_id',$shop['id'])->where('type',0)->order("id desc")->limit(1)->column('pic') ?:"";
            $info['shop_pic_1'] = Db::name('shop_pic')->where('shop_id',$shop['id'])->where('type',1)->column('pic') ?:"";
            $info['shop_pic_2'] = Db::name('shop_pic')->where('shop_id',$shop['id'])->where('type',2)->order("id desc")->limit(1)->column('pic') ?:"";
            $info['shop_pic_3'] = Db::name('shop_pic')->where('shop_id',$shop['id'])->where('type',3)->order("id desc")->limit(1)->column('pic') ?:"";
            $info['shop_pic_4'] = Db::name('shop_pic')->where('shop_id',$shop['id'])->where('type',4)->order("id desc")->limit(1)->column('pic') ?:"";

            unset($info['password']);
        }elseif($seller){
            $info = $seller;
            unset($info['password']);
        }else{
            $info = [];
        }
        return suc(['info'=>$info]);
    }
    //银行列表
    public function banklists()
    {
        //数据验证
        $d = request()->post();
        $rule = [
            'page'=>'require|integer',
            'limit'=>'require|integer',
        ];
        $msg = [];
        $r = $this->validate($d, $rule,$msg);
        if($r!==true) return err($r);

        $res = model('comBank')->order('id desc')->paginate($d['limit'],false,['page'=>$d['page']]);
        $rs = $res->toArray();
       	if($rs)
       		return suc($rs['data'], $rs['total']);
        return suc([],0);
    }
}
